export const getLocal = (key : string) : string | null => {
  return localStorage.getItem(key)
}

export const setLocal = (key :string, value : string) : void => {
  localStorage.setItem(key, value)
}

export const removeLocal = (key : string) : void => {
  localStorage.removeItem(key)
}

export const setSessionStorage = (key : string, value : string) : void => {
  sessionStorage.setItem(key, value)
}

export const getSessionStorage = (key : string) : string | null => {
  return sessionStorage.getItem(key)
}

// 默认数据
export const getMultiDefaultStocks = () : string => {
  return getIndexName().join(";");
}

export const getIndexName = () :string[] => {
  return ["上证指数", "深证成指","创业板","上证50","沪深300","科创50","中证500","中证1000", "中证全指"]
}

export const getIndexETFName = () :string[] => {
  return ["上证指数ETF", "创业板ETF","上证50ETF","沪深300ETF","科创50ETF"]
}


export const getLpClassName = (levelPoint: number) : string=> {
  if (levelPoint <= 10) {
    return 'lplt10-cell'
  } else if (levelPoint <= 20) {
    return 'lplt20-cell'
  } else if (levelPoint >= 99) {
    return 'lpgt99-cell'
  }
  return ""
}

export const getWuguClassName = (wugu : number) : string => {
  if ( wugu > 0 && wugu <= 10 ) {
      return 'wugu-lt10-cell'
  } else if ( wugu > 10 && wugu <= 100 ) {
      return 'wugu-lt100-cell'
  } else if ( wugu >= 1000 ) {
      return 'wugu-gt1000-cell'
  }
  return ""
}

export const getIncNumClassName = (incNum : number) : string => {
  if (incNum <= -4) {
      return 'wugu-lt10-cell'
  } else if (incNum == -3) {
    return 'lplt10-cell'
  }
  return ""
}

export function formatCurrency(amount : number, currencyCode = 'CNY') {
  const formatter = new Intl.NumberFormat('zh-CN', {
    style: 'currency',
    currency: currencyCode,
  });

  return formatter.format(amount);
}

const daysOfWeek = ['周日', '周一', '周二', '周三', '周四', '周五', '周六'];
export const getDayOfWeek = (dateStr : string) : string => {
  const [year, month, day] = dateStr.split('-').map(Number);

  let date = new Date(year, month - 1, day); // 注意：月份是从0开始的
  return daysOfWeek[date.getDay()];
}

export const formatDate = (date : Date) : string => {
  const year = date.getFullYear();
  const month = String(date.getMonth() + 1).padStart(2, '0');
  const day = String(date.getDate()).padStart(2, '0');

  return `${year}-${month}-${day}`;
}

export const getNowMonth = () : string => {
  const now = new Date();
  const year = now.getFullYear();
  const month = String(now.getMonth() + 1).padStart(2, '0');

  return `${year}--${month}`;
}

export const operationToColor = (operation : string) : string => {
  switch (operation) {
    case "买入":
    case "模拟买入":
      return "rgb(240, 250, 165)";
    case "卖出":
    case "模拟卖出":
      return "rgb(186, 246, 164)";
    case "分红":
      return "rgb(249, 184, 196)";
    default:
      return "rgb(240, 250, 165)";
  }
}

export const numberFormat = (value : number, num : number) : number => {
  return Math.round(value * Math.pow(10, num)) / Math.pow(10, num);
}

// 生成 0 到 n-1 的范围
export const range = (n : number) => Array.from({ length: n }, (_, i) => i);


export const calScreenWidth = () : String => {
  return Math.min(window.screen.width, 1600) - 50 + "px"
}